<template>
  <div class="SJ_UP">
    <div class="title">草稿箱</div>
    <div class="query" v-if="false">
      事件分类：
      <el-select
        v-model="query.filter.typeCode"
        filterable
        placeholder="请选择"
        size="small"
        @change="initTable(true)"
      >
        <el-option
          v-for="item in options_sjlx"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-button
        size="small"
        icon="el-icon-search"
        type="primary"
        style="margin-left: 5px"
        @click="initTable(true)"
        >搜索</el-button
      >
    </div>
    <div class="main">
      <el-table
        :data="tableData"
        border
        height="700"
        style="width: 100%"
        :header-cell-style="{
          background: 'rgb(246,250,253)',
          color: '#000000',
        }"
      >
        <el-table-column prop="eventTypeName" label="事件名称">
        </el-table-column>
        <el-table-column prop="data.eventno" label="事件编码">
        </el-table-column>
        <el-table-column prop="data.createDate" label="创建日期">
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-s-order"
              type="warning"
              size="small"
              @click="to_open(scope.row)"
              >继续上报</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="text-align: center; margin-top: 5px">
        <el-pagination
          layout="prev, pager, next"
          :page-size="query.pageSize"
          :current-page="query.currentPage"
          :total="total"
          @current-change="page_change"
        >
        </el-pagination>
      </div>
    </div>
    <!-- 弹层 -->
    <el-dialog
      :title="titleText"
      :visible.sync="dialogVisible"
      width="55%"
      :before-close="handleClose"
      :append-to-body="true"
      :close-on-click-modal="false"
    >
      <el-form
        label-position="right"
        label-width="150px"
        :model="formLabelAlign"
      >
        <el-form-item label="事件编码：">
          <el-input v-model="formLabelAlign.code"></el-input>
        </el-form-item>
        <el-form-item label="事件名称：">
          <el-input v-model="formLabelAlign.name"></el-input>
        </el-form-item>
        <el-form-item label="所属事件类型：">
          <el-select
            v-model="formLabelAlign.type"
            filterable
            placeholder="请选择"
            size="small"
            style="width: 100%"
          >
            <el-option
              v-for="item in options_sjlx"
              :key="item.value"
              :label="item.label"
              :value="item.value + ''"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="事件流程设置：">
          <el-select
            v-model="formLabelAlign.procdefid"
            filterable
            placeholder="请选择"
            size="small"
            style="width: 100%"
          >
            <el-option
              v-for="item in options_SJLC"
              :key="item.value"
              :label="item.flowname"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结案科室设置：">
          <el-select
            v-model="formLabelAlign.closedept"
            filterable
            placeholder="请选择"
            size="small"
            style="width: 100%"
          >
            <el-option
              v-for="item in options_KS"
              :key="item.value"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="BTN_OK1">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="事件元素设置"
      :visible.sync="dialogVisible_a"
      width="65%"
      :before-close="handleClose_a"
      :append-to-body="true"
      :close-on-click-modal="false"
    >
      <div style="text-align: right">
        <el-button
          type="primary"
          plain
          size="mini"
          icon="el-icon-plus"
          @click="addPlus"
          >添加行</el-button
        >
        <el-button
          type="primary"
          plain
          size="mini"
          icon="el-icon-minus"
          @click="del_row"
          >删除行</el-button
        >
      </div>
      <el-table
        :data="tableData_a"
        border
        height="400"
        style="width: 100%"
        :header-cell-style="{
          background: 'rgb(246,250,253)',
          color: '#000000',
        }"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="45"> </el-table-column>
        <el-table-column type="inputno" label="NO" width="80">
          <template slot-scope="scope">
            <el-input v-model="scope.row.inputno"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="code" label="元素标识" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.code"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="元素描述"
          align="center"
          width="220"
        >
          <template slot-scope="scope">
            <el-input v-model="scope.row.name"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          prop="inputtype"
          label="输入方式"
          align="center"
          width="160"
        >
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.inputtype"
              filterable
              placeholder="请选择"
              size="small"
              style="width: 120px"
            >
              <el-option
                v-for="item in options_SRFS"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <!-- <el-table-column
          prop="address"
          label="打印方式"
          align="center"
          width="120"
        >
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.address"
              filterable
              placeholder="请选择"
              size="small"
              style="width:80px"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column> -->
        <el-table-column
          prop="isnull
"
          label="是否必填"
          align="center"
          width="140"
        >
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.isnull"
              filterable
              placeholder="请选择"
              size="small"
              style="width: 100px"
            >
              <el-option label="必填" :value="true"> </el-option>
              <el-option label="非必填" :value="false"> </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column
          prop="typecode"
          label="数据字典"
          align="center"
          width="160"
        >
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.typecode"
              filterable
              placeholder="请选择"
              size="small"
              style="width: 140px"
            >
              <el-option
                v-for="item in options_ZDLX"
                :key="item.value"
                :label="item.typename"
                :value="item.typecode"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column
          prop="groupname"
          label="分组名称"
          align="center"
          width="220"
        >
          <template slot-scope="scope">
            <el-input v-model="scope.row.groupname"></el-input>
          </template>
        </el-table-column>
        <!-- <el-table-column
          prop="address"
          label="打印显示"
          align="center"
          width="120"
        >
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.address"
              filterable
              placeholder="请选择"
              size="small"
              style="width:80px"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column> -->
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose_a">取 消</el-button>
        <el-button type="primary" @click="BTN_OK2">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="事件鱼骨图分析设置"
      :visible.sync="dialogVisible_b"
      width="65%"
      :before-close="handleClose_b"
      :append-to-body="true"
      :close-on-click-modal="false"
    >
      <div style="height: 500px; overflow: auto">
        <el-tree
          ref="tree"
          :data="YGT_list"
          show-checkbox
          node-key="code"
          :default-checked-keys="YG_list"
          :props="defaultProps"
          @check="tree_change"
        >
        </el-tree>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose_b">取 消</el-button>
        <el-button type="primary" @click="tree_click">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  // 组件名称
  name: "",
  // 局部注册的组件
  components: {},
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data() {
    return {
      tableData: [],
      formLabelAlign: {},
      dialogVisible: false,
      dialogVisible_a: false,
      dialogVisible_b: false,
      YGT_list: [],
      titleText: "",
      tableData_a: [
        {
          date: "测试数据测试数据",
          address: "123",
        },
      ],
      codes: [],
      options: [],
      options_sjlx: [
        { value: 1001, label: "医疗管理类" },
        { value: 1002, label: "药品管理类" },
        { value: 1003, label: "护理管理类" },
        { value: 1004, label: "输血类" },
        { value: 1005, label: "院感类" },
        { value: 1006, label: "器械类" },
        { value: 1007, label: "后勤管理类" },
        { value: 1008, label: "治安管理类" },
        { value: 1009, label: "信息管理类" },
        { value: 1014, label: "行政其他类" },
        { value: 1015, label: "医技管理类" },
        { value: 1016, label: "食品安全类" },
        { value: 1017, label: "职业防护类" },
        { value: 1018, label: "信息安全类" },
        { value: 1019, label: "药品管理类" },
        { value: 1020, label: "化妆品管理类" },
        { value: 1021, label: "医疗器械管理类" },
        { value: 2001, label: "护理事件相关信息收集表" },
      ],
      options_KS: [],
      options_SJLC: [],
      options_ZDLX: [],
      options_SRFS: [
        { value: 1, label: "单行文本框" },
        { value: 2, label: "下拉框" },
        { value: 3, label: "横向多选" },
        { value: 4, label: "横向单选" },
        { value: 5, label: "多行文本域" },
        { value: 6, label: "日期选择框" },
        { value: 7, label: "鱼骨图" },
        { value: 9, label: "竖向单选" },
      ],
      value: "",
      data: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
      time: [],
      query: {
        currentPage: 1,
        pageSize: 15,
        filter: {
          key: "",
          withDisable: true,
          typeCode: "",
          typeName: "",
          startTime: "",
          endTime: "",
          delFlag: false,
        },
      },
      total: 0,
      PCode: "",
      YG_list: [],
      eventCode: "",
    };
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 生命周期钩子   注：没用到的钩子请自行删除
  /**
   * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
   */
  created() {},
  /**
   * el 被新创建的 vm.el 替换，并挂载到实例上去之后调用该钩子。
   * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.el 也在文档内。
   */
  mounted() {
    this.initTable();
  },
  // 组件方法
  methods: {
    open(flag, val) {
      if (flag == "add") {
        this.titleText = "新增事件分类维护";
      }
      if (flag == "edit") {
        this.titleText = "编辑事件分类维护";
        this.formLabelAlign = val;
      }
      if (flag == "stop") {
        this.formLabelAlign = val;
        this.formLabelAlign.delFlag = true;
        this.$put("/Aers/ZdEvent/Update", this.formLabelAlign);
        this.initTable();
        this.$message.success(
          "操作成功，状态不会马上改变，请手动刷新列表后查看！"
        );
        return;
      }
      if (flag == "start") {
        this.formLabelAlign = val;
        this.formLabelAlign.delFlag = false;
        this.$put("/Aers/ZdEvent/Update", this.formLabelAlign);
        setTimeout((_) => {
          this.initTable();
        }, 1000);
        this.$message.success(
          "操作成功，状态不会马上改变，请手动刷新列表后查看！"
        );
        return;
      }
      this.dialogVisible = true;
    },
    handleClose() {
      this.dialogVisible = false;
      this.formLabelAlign = {};
    },
    handleClose_a() {
      this.dialogVisible_a = false;
    },
    handleClose_b() {
      this.dialogVisible_b = false;
    },
    handleSelectionChange(val) {
      this.codes = val;
    },
    addPlus() {
      this.tableData_a.push({});
    },
    del_row() {
      var that = this;
      this.codes.forEach((item) => {
        that.tableData_a.forEach((item2, index) => {
          if (item.id == item2.id) {
            this.tableData_a.splice(index, 1);
          }
        });
      });
    },
    time_change() {
      this.query.filter.startTime = this.time[0];
      this.query.filter.endTime = this.time[1];
      this.initTable();
    },
    initTable(flag) {
      if (flag) {
        this.query.currentPage = 1;
      }
      //   var body = this.query;
      var body = {
        currentPage: 1,
        pageSize: 999,
        filter: {
          key: "",
          withDisable: "true",
        },
      };
      this.$post("/Aers/ReportEventmainTemp/GetList", body).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data.list;
          this.total = res.data.total;
        }
      });
    },
    page_change(val) {
      this.query.currentPage = val;
      this.initTable();
    },
    BTN_OK1() {
      var body = this.formLabelAlign;
      if (this.formLabelAlign.id) {
        this.$put("/Aers/ZdEvent/Update", body).then((res) => {
          if (res.code == 200) {
            this.handleClose();
            this.$message.success("操作成功");
          }
        });
      } else {
        this.$post("/Aers/ZdEvent/Create", body).then((res) => {
          if (res.code == 200) {
            this.handleClose();
            this.$message.success("操作成功");
          }
        });
      }
    },
    del(id) {
      this.$deleteF("/Aers/ZdEvent/Delete?id=" + id).then((res) => {
        if (res.code == 200) {
          this.initTable();
          this.$message.success("删除成功");
        }
      });
    },
    ZD_seting(code) {
      this.PCode = code;
      this.$post("/Aers/ZdElement/GetAll", { eventCode: code }).then((res) => {
        if (res.code == 200) {
          this.tableData_a = res.data;
          this.dialogVisible_a = true;
        }
      });
    },
    YGT_seting(code) {
      this.eventCode = code;
      var body = {
        eventCode: code,
      };
      this.PCode = code;
      this.$post("/Aers/ZdYgt/GetAllTree", { eventCode: code }).then((res) => {
        if (res.code == 200) {
          this.YGT_list = res.data;
          this.dialogVisible_b = true;
        }
      });
      this.$post("/Aers/ZdEventYgt/GetAll", body).then((res) => {
        this.YG_list = res.data;
      });
    },
    BTN_OK2() {
      var body = {
        eventCode: this.PCode,
        list: this.tableData_a,
      };
      this.$post("/Aers/ZdElement/SetElement", body).then((res) => {
        if (res.code == 200) {
          this.$message.success("操作成功！");
          this.handleClose_a();
        }
      });
    },
    tree_change(val) {
      console.log(val);
      // var list = [];
      // for (var i in val) {
      //   list.pust(val[i].code);
      //   if (val[i].children) {
      //     for (var j in val[i].children) {
      //       list.pust(val[i].children[j].code);
      //       if (val[i].children[j].children) {
      //         for (var k in val[i].children[j].children) {
      //           list.pust(val[i].children[j].children[k].code);
      //         }
      //       }
      //     }
      //   }
      // }
      // console.log(list);
    },
    tree_click() {
      let arr = this.$refs.tree.getCheckedKeys();
      var body = {
        eventCode: this.eventCode,
        ygtCodes: arr,
      };
      this.$post("/Aers/ZdEventYgt/SetEventYgt", body).then((res) => {
        this.dialogVisible_b = false;
        this.$message.success("操作成功！");
      });
    },
    to_open(row) {
      console.log(row);
      this.$router.push(
        "/operation?eventCode=" +
          row.data.eventcode +
          "&eventName=" +
          row.eventTypeName +
          "&Token=" +
          localStorage.getItem("Token") +
          "&flag=true&id=" +
          row.data.id +
          "&eventno=" +
          row.data.eventno
      );
    },
  },
};
</script>

<style scoped lang="scss">
.SJ_UP {
  .title {
    background-color: rgb(233, 242, 255);
    line-height: 45px;
    font-size: 18px;
    font-weight: 700;
    padding: 0 10px;
  }
  .query {
    margin: 0 10px;
    margin-top: 10px;
    padding: 5px;
    border: 1px solid #ccc;
    line-height: 40px;
  }
  .main {
    padding: 0 10px;
    margin-top: 10px;
  }
  .input_style {
    display: inline-block;
    width: 200px;
  }
}
</style>
